﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CommonLibrary
{
    public sealed class WinAPI
    {
        [DllImport("gdi32.dll")]
        public static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("gdi32.dll")]
        public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(IntPtr hDC);

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteDC(IntPtr hDC);


        /// <summary>
        /// CreateCompatibleBitmap
        /// </summary>
        /// <param name="hDC"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, int nHeight);

        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool DeleteObject(IntPtr hgdiobj);


        public const uint SRCCOPY = 0x00CC0020u; /* dest = source                   */
        public const uint SRCPAINT = 0x00EE0086u; /* dest = source OR dest           */
        public const uint SRCAND = 0x008800C6u; /* dest = source AND dest          */
        public const uint SRCINVERT = 0x00660046u; /* dest = source XOR dest          */
        public const uint SRCERASE = 0x00440328u; /* dest = source AND (NOT dest )   */
        public const uint NOTSRCCOPY = 0x00330008u; /* dest = (NOT source)             */
        public const uint NOTSRCERASE = 0x001100A6u; /* dest = (NOT src) AND (NOT dest) */
        public const uint MERGECOPY = 0x00C000CAu; /* dest = (source AND pattern)     */
        public const uint MERGEPAINT = 0x00BB0226u; /* dest = (NOT source) OR dest     */
        public const uint PATCOPY = 0x00F00021u; /* dest = pattern                  */
        public const uint PATPAINT = 0x00FB0A09u; /* dest = DPSnoo                   */
        public const uint PATINVERT = 0x005A0049u; /* dest = pattern XOR dest         */
        public const uint DSTINVERT = 0x00550009u; /* dest = (NOT dest)               */
        public const uint BLACKNESS = 0x00000042u; /* dest = BLACK                    */
        public const uint WHITENESS = 0x00FF0062u; /* dest = WHITE                    */

        /// <summary>
        /// 贴图
        /// </summary>
        /// <param name="hdcDest"></param>
        /// <param name="nXDest"></param>
        /// <param name="nYDest"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <param name="hdcSrc"></param>
        /// <param name="nXSrc"></param>
        /// <param name="nYSrc"></param>
        /// <param name="dwRop"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);


        public struct TRIVERTEX
        {
            public int x;
            public int y;
            public ushort Red;
            public ushort Green;
            public ushort Blue;
            public ushort Alpha;

            /// <summary>
            /// 构造
            /// </summary>
            /// <param name="x"></param>
            /// <param name="y"></param>
            /// <param name="red"></param>
            /// <param name="green"></param>
            /// <param name="blue"></param>
            /// <param name="alpha"></param>
            public TRIVERTEX(int x, int y, ushort red, ushort green, ushort blue, ushort alpha)
            {
                this.x = x;
                this.y = y;
                this.Red = (ushort)(red << 8);
                this.Green = (ushort)(green << 8);
                this.Blue = (ushort)(blue << 8);
                this.Alpha = (ushort)(alpha << 8);
            }
        }

        public struct GRADIENT_RECT
        {
            public uint UpperLeft;
            public uint LowerRight;

            /// <summary>
            /// 构造
            /// </summary>
            /// <param name="ul"></param>
            /// <param name="lr"></param>
            public GRADIENT_RECT(uint ul, uint lr)
            {
                this.UpperLeft = ul;
                this.LowerRight = lr;
            }
        }

        public const int GRADIENT_FILL_RECT_H = 0x00000000;
        public const int GRADIENT_FILL_RECT_V = 0x00000001;

        /// <summary>
        /// GradientFill
        /// </summary>
        /// <param name="hdc"></param>
        /// <param name="pVertex"></param>
        /// <param name="dwNumVertex"></param>
        /// <param name="pMesh"></param>
        /// <param name="dwNumMesh"></param>
        /// <param name="dwMode"></param>
        /// <returns></returns>
        [DllImport("msimg32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public extern static bool GradientFill(IntPtr hdc, TRIVERTEX[] pVertex, uint dwNumVertex, GRADIENT_RECT[] pMesh, uint dwNumMesh, uint dwMode);

        /// <summary>
        /// RoundRect
        /// </summary>
        /// <param name="hdc"></param>
        /// <param name="nLeftRect"></param>
        /// <param name="nTopRect"></param>
        /// <param name="nRightRect"></param>
        /// <param name="nBottomRect"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <returns></returns>
        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);


        public const int WHITE_BRUSH = 0;
        public const int LTGRAY_BRUSH = 1;
        public const int GRAY_BRUSH = 2;
        public const int DKGRAY_BRUSH = 3;
        public const int BLACK_BRUSH = 4;
        public const int NULL_BRUSH = 5;
        public const int HOLLOW_BRUSH = 5;
        public const int WHITE_PEN = 6;
        public const int BLACK_PEN = 7;
        public const int NULL_PEN = 8;
        public const int SYSTEM_FONT = 13;
        public const int DEFAULT_PALETTE = 15;
        public const int BORDERX_PEN = 32;
        public const int BORDERY_PEN = 33;


        [DllImport("gdi32.dll")]
        public static extern IntPtr GetStockObject(int fnObject);

        public const int PS_SOLID = 0;
        public const int PS_DASH = 1;
        public const int PS_NULL = 5;

        public static uint RGB(byte r, byte g, byte b)
        {
            return (uint)r | (uint)((uint)g << 8) | (uint)((uint)b << 16);
        }

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreatePen(int fnPenStyle, int nWidth, uint crColor);

        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateSolidBrush(uint crColor);


        public class RECT
        {
            int left;
            int top;
            int right;
            int bottom;

            public RECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }


        [DllImport("gdi32.dll")]
        public static extern int FillRect(IntPtr hDC, RECT lprc, IntPtr hbr);


    }
}
